import { AbNames } from "../../../Game/Scripts/GameCfg";

/**
 * 资源管理器
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class ResMgr extends cc.Component {

    public static Instance: ResMgr = null;
    onLoad() {
        if (ResMgr.Instance !== null) {
            this.destroy();
            return;
        }
        ResMgr.Instance = this;
    }

    start() {

    }

    public async Init() {
        console.log("========ResMgr Init===========");

        // var bundle:cc.AssetManager.Bundle  = await this.IE_LoadBundle(AbNames.Game) as cc.AssetManager.Bundle;
        // console.log("======ResMgr=====",bundle);
        // let ass = await this.IE_LoadAllAssetInBundle(bundle,TextAsset);
        // console.log('=======ass========',ass);
        // ass = await this.IE_LoadAssetInBundle(bundle,'fragment',TextAsset);
        // console.log('=======ass1========',ass);
    }


      /**
     *   异步加载bundle
     */
      public async IE_LoadBundle(bundleName: string) {
        return new Promise((resolve, reject)=>{
            cc.assetManager.loadBundle(bundleName,(err, bundleData)=>{
                if(err){
                    console.log(err);
                    reject(null);
                    return;
                }
                else{
                    resolve(bundleData);
                    return;
                }
            })
        })
    }

    /**
     *加载bundle 里面的所有资源
     * @param bundle 
     * @param assetType 
     * @returns 
     */
    private async IE_LoadAllAssetInBundle(bundle: cc.AssetManager.Bundle, assetType) {
        return new Promise((resolve,reject)=>{
            bundle.loadDir('',assetType,(err, infos)=>{
                if(err){
                    reject(err);
                    return;
                }
                else{
                    resolve(infos);
                }
            })
        })
    }

    /**
     * 加载bundle 里面的单个资源
     * @param bundle 
     * @param assetName 
     * @param assetType 
     * @returns 
     */
    private async IE_LoadAssetInBundle(bundle:cc.AssetManager.Bundle, assetName: string, assetType){
        return new Promise((resolve, reject)=>{
            bundle.load(assetName, assetType,(err, assetData)=>{
                if(err){
                    reject(err);
                    return;
                }
                else{
                    resolve(assetData);
                }
            })
        })
    }

    /**
     * 加载bundle 里面的单个场景资源
     * @param bundle 
     * @param sceneName 
     * @returns 
     */
    private async IE_LoadSceneInBundle(bundle: cc.AssetManager.Bundle, sceneName: string) {
        return new Promise((resolve, reject)=>{
            bundle.loadScene(sceneName,(err,sceneData)=>{
                if(err){
                    reject(err);
                    return;
                }
                else{
                    resolve(sceneData);
                    return;
                }
            });
        })
    }

    /**
     * 加载bundle 里面的所有资源
     * @param bundleName ab包名
     * @param assetType 资源类型
     * @returns 
     */
    public async IE_LoadBundleAndAllAssets(bundleName: string, assetType){
        var bundle: cc.AssetManager.Bundle = await this.IE_LoadBundle(bundleName) as cc.AssetManager.Bundle;
        if(bundle === null){
            return null;
        }

        var assetDatas =  await this.IE_LoadAllAssetInBundle(bundle,assetType);
        return assetDatas;
    }

    /**
     * 加载bundle 里面的单个场景资源
     * @param bundleName ab包名
     * @param scenePath 资源路径
     * @returns 
     */
    public async IE_GetScene(bundleName: string, scenePath: string){
        var bundle: cc.AssetManager.Bundle = cc.assetManager.getBundle(bundleName);
        if(bundle === null) {
            bundle = await this.IE_LoadBundle(bundleName) as any;
            if(bundle === null) {
                console.log("bundle load err: " + bundleName);
                return;
            }
        }
        var sceneData = await this.IE_LoadSceneInBundle(bundle, scenePath) as any;
        return sceneData;
    }

    /**
     * 获取指定bundle里面的指定资源
     * @param bundleName ab包名
     * @param assetPath 资源路径
     * @param assetType 资源类型
     * @returns 
     */
    public async IE_GetAsset(bundleName: string, assetPath: string, assetType) {
        var bundle: cc.AssetManager.Bundle = cc.assetManager.getBundle(bundleName);
        if(bundle === null) {
            bundle = await this.IE_LoadBundle(bundleName) as any;
            if(bundle === null) {
                // console.log("bundle load err: " + bundleName);
                return;
            }
        }

        var assetData = bundle.get(assetPath);
        if(assetData) {
            return assetData;
        }

        assetData = await this.IE_LoadAssetInBundle(bundle, assetPath, assetType) as any;
        return assetData;
    }

    /***
     * 
     */
    public ReleaseAsset(assetData: cc.Asset): void {
        cc.assetManager.releaseAsset(assetData);
    }

    /**
     * 释放bundle 里面的所有资源
     * @param bundleName 
     * @returns 
     */
    public ReleaseAllAssetInBundle(bundleName: string): void {
        var bundle: cc.AssetManager.Bundle = cc.assetManager.getBundle(bundleName);
        if(bundle === null) {
            return;
        }

        bundle.releaseAll();

        cc.assetManager.removeBundle(bundle);
    } 
}
